﻿using BLL;
using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MachineDemo
{
    public partial class OrderForm : Form
    {
        public OrderForm()
        {
            InitializeComponent();
            dgv_OrderData.AutoGenerateColumns = false;//表格取消自动更新列

            dgv_OrderData.DataSource = LogicSingle.Instaince._OrderMessageObj;

        }

        private void btn_UpdateStartWorkData_Click(object sender, EventArgs e)
        {
            txb_StartWorkOrderNo.Text = LogicSingle.Instaince._StartMessageObj.WorkOrderNo;
            txb_StartStationNo.Text = LogicSingle.Instaince._StartMessageObj.StationNo;
            txb_StartSerialNo.Text = LogicSingle.Instaince._StartMessageObj.SerialNo;

            string CatchPlaceNoMessage = "";
            foreach (var item in LogicSingle.Instaince._StartMessageObj.CatchPlaceNo)
            {
                CatchPlaceNoMessage = CatchPlaceNoMessage + item + ",";
            }
            CatchPlaceNoMessage = CatchPlaceNoMessage.Substring(0, CatchPlaceNoMessage.Length - 1);
            txb_StartCatchPlaceNo.Text = CatchPlaceNoMessage;
        }

        private void btn_UpdateReportWorkData_Click(object sender, EventArgs e)
        {
            txb_ReportWorkOrderNo.Text = LogicSingle.Instaince._ReportMessageObj.WorkOrderNo;
            txb_ReportStationNo.Text = LogicSingle.Instaince._ReportMessageObj.StationNo;
            txb_ReportTargetPlaceNo.Text = LogicSingle.Instaince._ReportMessageObj.TargetPlaceNo;
            txb_ReportGoodQty.Text = LogicSingle.Instaince._ReportMessageObj.GoodQty;
            txb_ReportBadQty.Text = LogicSingle.Instaince._ReportMessageObj.BadQty;
            txb_ReportSerialNo.Text = LogicSingle.Instaince._ReportMessageObj.SerialNo;

            string ReportCatchPlaceNoMessage = "";
            foreach (var item in LogicSingle.Instaince._ReportMessageObj.CatchPlaceNo)
            {
                ReportCatchPlaceNoMessage = ReportCatchPlaceNoMessage + item + ",";
            }
            if (ReportCatchPlaceNoMessage!="")
            {
                txb_ReportCatchPlaceNo.Text = ReportCatchPlaceNoMessage.Substring(0, ReportCatchPlaceNoMessage.Length - 1);
            }
           
            //制造参数
            string ReportManufacturingItemsMessage = "";
            foreach (var item in LogicSingle.Instaince._ReportMessageObj.ManufacturingItems)
            {
                ReportManufacturingItemsMessage += "(" + item.Key + "," + item.Value + ")";
            }
            txb_ReportManufacturingItems.Text = ReportManufacturingItemsMessage;


            //验证参数
            string ReportInspectionItemsMessage = "";
            foreach (var item in LogicSingle.Instaince._ReportMessageObj.InspectionItems)
            {
                ReportInspectionItemsMessage += "(" + item.Key + "," + item.Value + ")";
            }
            txb_ReportInspectionItems.Text = ReportInspectionItemsMessage;


            //抓取物料详情
            string CatchDataMessage = "";
            foreach (var item in LogicSingle.Instaince._ReportMessageObj.CatchData)
            {
                CatchDataMessage += "(" + item.CatchPlaceNo + "," + item.CatchQty + "," + item.MaterialCodification + ")";
            }
            txb_ReportCatchData.Text = CatchDataMessage;
        }

        private void btn_StartWorkSend_Click(object sender, EventArgs e)
        {
            if (txb_StartWorkOrderNo.Text == "" || txb_StartStationNo.Text == "" || txb_StartCatchPlaceNo.Text == "" || txb_StartSerialNo.Text == "")
            {
                MessageBox.Show("不能为空");
                return;
            }
            StartInterface TempObj = LogicSingle.Instaince._StartMessageObj;
            try
            {
                LogicSingle.Instaince._StartMessageObj.WorkOrderNo = txb_StartWorkOrderNo.Text;
                LogicSingle.Instaince._StartMessageObj.StationNo = txb_StartStationNo.Text;
                string[] CatchPlaceNoList = txb_StartCatchPlaceNo.Text.Split(',');
                LogicSingle.Instaince._StartMessageObj.CatchPlaceNo = CatchPlaceNoList;
                LogicSingle.Instaince._StartMessageObj.SerialNo = txb_StartSerialNo.Text;
                MessageBox.Show("写入成功");
            }
            catch (Exception)
            {
                LogicSingle.Instaince._StartMessageObj = TempObj;
                MessageBox.Show("写入失败");
            }

        }

        private void btn_ReportWorkSend_Click(object sender, EventArgs e)
        {
            if (txb_ReportWorkOrderNo.Text == "" || txb_ReportStationNo.Text == "" || txb_ReportTargetPlaceNo.Text == "" ||
                txb_ReportGoodQty.Text == "" || txb_ReportBadQty.Text == "" || txb_ReportSerialNo.Text == "" ||
                txb_ReportCatchPlaceNo.Text == "" || txb_ReportManufacturingItems.Text == "" || txb_ReportInspectionItems.Text == "" ||
                txb_ReportCatchData.Text == "")
            {
                MessageBox.Show("不能为空");
                return;
            }
            MESsignal TempObj = LogicSingle.Instaince._ReportMessageObj;
            try
            {
                LogicSingle.Instaince._ReportMessageObj.WorkOrderNo = txb_ReportWorkOrderNo.Text;
                LogicSingle.Instaince._ReportMessageObj.StationNo = txb_ReportStationNo.Text;
                LogicSingle.Instaince._ReportMessageObj.TargetPlaceNo = txb_ReportTargetPlaceNo.Text;
                LogicSingle.Instaince._ReportMessageObj.GoodQty = txb_ReportGoodQty.Text;
                LogicSingle.Instaince._ReportMessageObj.BadQty = txb_ReportBadQty.Text;
                LogicSingle.Instaince._ReportMessageObj.SerialNo = txb_ReportSerialNo.Text;

                //抓取区隔号
                string[] CatchPlaceNoList = txb_ReportCatchPlaceNo.Text.Split(',');
                LogicSingle.Instaince._ReportMessageObj.CatchPlaceNo = CatchPlaceNoList;


                //制造参数
                string ManufacturingItemsTemp = txb_ReportManufacturingItems.Text.Replace(")", "");
                ManufacturingItemsTemp = ManufacturingItemsTemp.Substring(1, ManufacturingItemsTemp.Length - 1);
                string[] ManufacturingItemList = ManufacturingItemsTemp.Split('(');
                LogicSingle.Instaince._ReportMessageObj.ManufacturingItems = new List<KeyValueInfo>();
                foreach (var item in ManufacturingItemList)
                {
                    item.Trim();
                    string[] ResList = item.Split(',');
                    LogicSingle.Instaince._ReportMessageObj.ManufacturingItems.Add(new KeyValueInfo() { Key = ResList[0], Value = ResList[1] });
                }


                //验证参数
                string InspectionItemsTemp = txb_ReportInspectionItems.Text.Replace(")", "");
                InspectionItemsTemp = InspectionItemsTemp.Substring(1, InspectionItemsTemp.Length - 1);
                string[] InspectionItemsList = InspectionItemsTemp.Split('(');
                LogicSingle.Instaince._ReportMessageObj.InspectionItems = new List<KeyValueInfo>();
                foreach (var item in InspectionItemsList)
                {
                    item.Trim();
                    string[] ResList = item.Split(',');
                    LogicSingle.Instaince._ReportMessageObj.InspectionItems.Add(new KeyValueInfo() { Key = ResList[0], Value = ResList[1] });
                }

                //抓取物料详情
                string ReportCatchDataTemp = txb_ReportCatchData.Text.Replace(")", "");
                ReportCatchDataTemp = ReportCatchDataTemp.Substring(1, ReportCatchDataTemp.Length - 1);
                string[] ReportCatchDataList = ReportCatchDataTemp.Split('(');
                LogicSingle.Instaince._ReportMessageObj.CatchData = new List<Catch>();
                foreach (var item in ReportCatchDataList)
                {
                    item.Trim();
                    string[] ResList = item.Split(',');
                    LogicSingle.Instaince._ReportMessageObj.CatchData.Add(new Catch() { CatchPlaceNo = ResList[0], CatchQty = Convert.ToInt32(ResList[1]), MaterialCodification = ResList[1] });
                }
                MessageBox.Show("写入成功");
            }
            catch (Exception)
            {
                LogicSingle.Instaince._ReportMessageObj = TempObj;
                MessageBox.Show("写入失败");
            }
        }

        private void btn_StartWorkTest_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince._PlcConnect.IsSuccess)
            {
                try
                {
                    LogicSingle.Instaince._PLCDevice.Write(LogicSingle.Instaince._PLCAddress.StartWorkState, 1);
                    LogSingle.Instaince.WriteMainLog("模拟报工成功");
                }
                catch (Exception)
                {
                    LogSingle.Instaince.WriteMainLog("模拟报工失败");
                }

            }
            else
            {
                MessageBox.Show("PLC未连接");
            }
        }

        private void btn_ReportWorkTest_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince._PlcConnect.IsSuccess)
            {
                try
                {
                    LogicSingle.Instaince._PLCDevice.Write(LogicSingle.Instaince._PLCAddress.ReportWorkState, 1);
                    LogSingle.Instaince.WriteMainLog("模拟报工成功");
                }
                catch (Exception)
                {
                    LogSingle.Instaince.WriteMainLog("模拟报工失败");
                }
            }
            else
            {
                MessageBox.Show("PLC未连接");
            }
        }

        private void btn_OrderTest_Click(object sender, EventArgs e)
        {
            LogicSingle.Instaince._PLCDevice.Write(LogicSingle.Instaince._PLCAddress.OrderNum, 1);
        }

        private void btn_WorkMessageClear_Click(object sender, EventArgs e)
        {
            LogicSingle.Instaince.IniWorkMessage();
        }

        private void btn_HandStartWork_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince.StartWork())
            {
                MessageBox.Show("发送成功");
            }
            else
            {
                MessageBox.Show("发送失败");
            }
        }

        private void btn_HandReportWork_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince.ReportWork())
            {
                MessageBox.Show("发送成功");
            }
            else
            {
                MessageBox.Show("发送失败");
            }
        }

        private void btn_AGV1Up_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince._UperComputerClientent.IsConnection)
            {
                if (LogicSingle.Instaince.DispatchAGV("AGV1", "Up"))
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV1上料发送成功");
                }
                else
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV1上料发送失败");
                }
            }
            else
            {
                MessageBox.Show("未连接服务器");
            }
        }

        private void btn_AGV2Up_Click(object sender, EventArgs e)
        {

            if (LogicSingle.Instaince._UperComputerClientent.IsConnection)
            {
                if (LogicSingle.Instaince.DispatchAGV("AGV2", "Up"))
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV2上料发送成功");
                }
                else
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV2上料发送成功");
                }
               
            }
            else
            {
                MessageBox.Show("未连接服务器");
            }

        }

        private void btn_AGV1Down_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince._UperComputerClientent.IsConnection)
            {
                if (LogicSingle.Instaince.DispatchAGV("AGV1", "Down"))
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV1下料发送成功");
                }
                else
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV1下料发送失败");
                }
               
            }
            else
            {
                MessageBox.Show("未连接服务器");
            }
        }

        private void btn_AGV2Down_Click(object sender, EventArgs e)
        {
            if (LogicSingle.Instaince._UperComputerClientent.IsConnection)
            {
                if (LogicSingle.Instaince.DispatchAGV("AGV2", "Down"))
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV2下料发送成功");
                }
                else
                {
                    LogSingle.Instaince.WriteMainLogThing("AGV2下料发送失败");
                }
               
            }
            else
            {
                MessageBox.Show("未连接服务器");
            }
        }
    }
}
